/**
 * Created by zhubuqing on 2018/4/23.
 */
var addUrl = "/sizheng/html/";

$(function () {
    var urlAddress = window.location.href;

    // alert(urlAddress);

    if (urlAddress.search("sizheng/html") != -1) {
        addUrl = "";
    }

    // 获取Banner图
    getBanner();

    // 获取七本书
    getBookList();

    // 获取新闻热点
    $("#toXqListXwrd").html('<a href="' + addUrl + 'xq_list.html?type=shouYe_xinWenReDian">更多</a>');
    getCommonNewsXinWenReDian();

    // 获取精彩一课
    $("#toXqListJcyk").html('<a href="' + addUrl + 'xq_list.html?type=shouYe_jingCaiYiKe">更多</a>');
    getCommonResourceJingCaiYiKe();

    // 获取名校动态
    $("#toXqListMxdt").html('<a href="' + addUrl + 'xq_list.html?type=shouYe_mingXiaoDongTai">更多</a>');
    getSchoolDynamicsMingXiaoDongTai();

    // 获取入驻名师
    $("#toXqListRzms").html('<a href="' + addUrl + 'xq_list.html?type=shouYe_ruZhuMingShi">更多</a>');
    getTeacherRuZhuMingShi();

    // 获取合作院校
    $("#toXqListHzyx").html('<a href="' + addUrl + 'xq_list.html?type=gxjl_main_zhongDianMaYuan">更多</a>');
    getSchoolHeZuoYuanXiao();
});

// 获取Banner
function getBanner() {
    var data = {
        type: "FIRST_PAGE"
    };
    AjaxSubmit("GET", data, basePath + "/common/firstPage/getBanner", getBanner_fun);
}
function getBanner_fun(res) {
    // console.log(res);
    if (res.status == "SUCCESS") {
        var imgLists = res.obj;
        $.each(imgLists, function (i, imgList) {
            if (i == 0) {
                $(".carousel-indicators").append('<li data-target="#carousel-example-generic" data-slide-to="' + i + '" class="active"></li>');
                $(".carousel-inner").append('<div class="item active"> ' +
                    '<a href="' + imgList.url + '" target="_blank"><img src="' + imgList.pic + '" alt="..." style="height:460px;width:100%"></a>' +
                    '<div class="carousel-caption">' +
                    imgList.title +
                    '</div> ' +
                    '</div>');
            }
            else {
                $(".carousel-indicators").append('<li data-target="#carousel-example-generic" data-slide-to="' + i + '"></li>');
                $(".carousel-inner").append('<div class="item"> ' +
                    '<a href="' + imgList.url + '" target="_blank"><img src="' + imgList.pic + '" alt="..." style="height:460px;width:100%"></a>' +
                    '<div class="carousel-caption">' +
                    imgList.title +
                    '</div> ' +
                    '</div>');
            }
        })
    }
}

// 获取七本书
function getBookList() {
    var data = {};
    AjaxSubmit("GET", data, basePath + "/common/resource/getBookList", getBookList_fun);
}
function getBookList_fun(res) {
    var BookLists = res.obj;
    if (res.status == "SUCCESS") {
        $.each(BookLists, function (i, BookList) {
            if (BookList.useType !== "THE_USE_OF_THE_TEACHING_MATERIALS_IN_THE_ELECTIVE_COURSE_OF_IDEOLOGICAL_AND_POLITICAL_AFFAIRS") {
                if (i == 4) {
                    $(".lessonDiv").find("ul").append('<li>' +
                        '<a href="' + addUrl + 'thematicClassification.html">' +
                        '<img src="' + BookList.pic + '" alt=""> ' +
                        '<p>' + BookList.bookName + '</p>' +
                        '</a>' +
                        '</li>');
                } else if (i == 5) {
                    $(".lessonDiv").find("ul").append('<li>' +
                        '<a href="' + addUrl + 'jx_zjjs.html">' +
                        '<img src="' + BookList.pic + '" alt="">' +
                        '<p>' + BookList.bookName + '</p>' +
                        '</a>' +
                        '</li>');
                } else {
                    $(".lessonDiv").find("ul").append('<li>' +
                        '<a href="' + addUrl + 'jx_fourBook.html?bookId=' + BookList.id + '">' +
                        '<img src="' + BookList.pic + '" alt="">' +
                        '<p>' + BookList.bookName + '</p>' +
                        '</a>' +
                        '</li>');
                }
            }
        })
    }
}

// 获取新闻热点
function getCommonNewsXinWenReDian() {
    var data = {
        status: "UP"
    };
    AjaxSubmit("GET", data, basePath + "/common/firstPage/searchCommonNews", getCommonNewsXinWenReDianRes);
}
function getCommonNewsXinWenReDianRes(res) {
    var resultData = res.obj.content;

    var xinWenReDianHtml = "";
    console.log(resultData)
    for (var i = 0; i < resultData.length; i++) {
        xinWenReDianHtml += '<a onclick=toXqNormal("' + resultData[i].id + '","shouYe_xinWenReDian")><div class="col-md-4">';
        xinWenReDianHtml += '<div class="imgBoxList">';
        xinWenReDianHtml += '<img src="' + resultData[i].pic + '" alt="" class="redianPic">'
        xinWenReDianHtml += '<p class="p1">' + resultData[i].title + '</p>';
        xinWenReDianHtml += '<p class="bt1">' + limitLength(123, resultData[i].abstractContent) + '</p>';
        xinWenReDianHtml += '</div>';
        xinWenReDianHtml += '</div></a>';
    }

    $("#xinWenReDian").html(xinWenReDianHtml);
}

// 获取精彩一课
function getCommonResourceJingCaiYiKe() {
    var data = {
        page: 0,
        size: 6,
        type: "WONDERFUL_LESSON",
        status: "UP"
    };
    AjaxSubmit("GET", data, basePath + "/common/firstPage/searchCommonResource", getCommonResourceJingCaiYiKeRes);
}
function getCommonResourceJingCaiYiKeRes(res) {
    var resultData = res.obj.content;

    var jingCaiYiKeHtml = "";

    var jingCaiYiKe2Html = "";

    for (var i = 0; i < resultData.length; i++) {
        if (resultData[i].field4 == "1") {
            jingCaiYiKeHtml += '<a href="' + resultData[i].fileUrl + '"><div class="col-md-2">';
        } else {
            jingCaiYiKeHtml += '<a onclick=toXqVideo("' + resultData[i].id + '","shouYe_jingCaiYiKe")><div class="col-md-2">';
        }
        jingCaiYiKeHtml += '<div class="imgBoxList">';
        jingCaiYiKeHtml += '<img src="' + resultData[i].pic + '" alt="" style="height:100px;">';
        jingCaiYiKeHtml += '<p class="p1">' + resultData[i].title + '</p>';
        jingCaiYiKeHtml += '<p class="jcyk"></p>';
        jingCaiYiKeHtml += '</div>';
        jingCaiYiKeHtml += '</div></a>';
    }

    $("#jingCaiYiKe").html(jingCaiYiKeHtml);
    $("#jingCaiYiKe2").html(jingCaiYiKe2Html);
}

// 获取名校动态1
function getSchoolDynamicsMingXiaoDongTai() {
    var data = {
        page: 0,
        size: 3,
        type: "DYNAMICS",
        status: "AUDITED",
        upDownType: "UP"
    };
    AjaxSubmit("GET", data, basePath + "/common/firstPage/searchSchoolDynamics", getSchoolDynamicsMingXiaoDongTaiRes);
}
function getSchoolDynamicsMingXiaoDongTaiRes(res) {
    var resultData = res.obj.content;

    var mingXiaoDongTaiHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        mingXiaoDongTaiHtml += '<a onclick=toXqNormal("' + resultData[i].id + '","shouYe_mingXiaoDongTai")><div class="col-md-4">';
        mingXiaoDongTaiHtml += '<div class="imgBoxList">';
        mingXiaoDongTaiHtml += '<img src="' + resultData[i].pic + '" alt="" class="redianPic">';
        mingXiaoDongTaiHtml += '<p class="p1">' + resultData[i].title + '</p>'
        mingXiaoDongTaiHtml += '<div class="bt1">' + limitLength(120, resultData[i].content) + '</div>';
        mingXiaoDongTaiHtml += '</div>';
        mingXiaoDongTaiHtml += '</div></a>';
    }

    $("#mingXiaoDongTai").html(mingXiaoDongTaiHtml);
}

// 获取入驻名师
function getTeacherRuZhuMingShi() {
    var data = {
        page: 0,
        size: 6,
        isFamousTeacher: 1
    };
    AjaxSubmit("GET", data, basePath + "/common/firstPage/searchTeacher", getTeacherRuZhuMingShiRes);
}
function getTeacherRuZhuMingShiRes(res) {
    var resultData = res.obj.content;

    var ruZhuMingShiHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        ruZhuMingShiHtml += '<a href="' + addUrl + 'ms_inside_watch.html?teacherId=' + resultData[i].id + '"><div class="col-md-2">';
        ruZhuMingShiHtml += '<div class="imgBoxList1">';
        ruZhuMingShiHtml += '<img src="' + resultData[i].pic + '" alt="..." style="width:120px;height:120px;border-radius:50%">';
        ruZhuMingShiHtml += '<p class="p2">' + resultData[i].teacherName + '</p>';
        ruZhuMingShiHtml += '<p class="bt2">' + resultData[i].schoolName + '（' + changeTeacherPositionalEnum(resultData[i].positional) + '）</p>';
        ruZhuMingShiHtml += '</div>';
        ruZhuMingShiHtml += '</div></a>';
    }

    $("#ruZhuMingShi").html(ruZhuMingShiHtml);
}

// 获取合作院校
function getSchoolHeZuoYuanXiao() {
    var data = {
        page: 0,
        size: 6
    };
    AjaxSubmit("GET", data, basePath + "/common/schoolCommunicate/getMarxSchool", getSchoolHeZuoYuanXiaoRes);
}
function getSchoolHeZuoYuanXiaoRes(res) {
    var resultData = res.obj.content;

    var heZuoYuanXiaoHtml = "";

    for (var i = 0; i < resultData.length; i++) {
//        console.log(resultData);
        if (resultData[i].url != null) {
            heZuoYuanXiaoHtml += '<a href="' + resultData[i].url + '" target="_blank"><div class="col-md-2">';
        } else {
            heZuoYuanXiaoHtml += '<a href="#"><div class="col-md-2">';
        }
        heZuoYuanXiaoHtml += '<div class="imgBoxList1">';
        heZuoYuanXiaoHtml += '<img src="' + resultData[i].pic + '" alt="...">';
        heZuoYuanXiaoHtml += '<p class="p2">' + resultData[i].marxSchool + '马克思主义学院</p>';
        heZuoYuanXiaoHtml += '</div></div></a>';
    }

    $("#heZuoYuanXiao").html(heZuoYuanXiaoHtml);
}

/********************************************** Util **********************************************/

// 限制字符串长度
function limitLength(wantLength, str) {
    var returnStr = "";
    var realLength = 0;
    var len = (str != null) ? str.length : 0;
    var charCode = -1;
    for (var i = 0; i < len; i++) {
        if (realLength < wantLength) {
            charCode = str.charCodeAt(i);
            if (charCode >= 0 && charCode <= 128)
                realLength += 1;
            else
                realLength += 2;
            returnStr += str.charAt(i);
        } else {
            returnStr += "...";
            break;
        }
    }
    return returnStr;
}

// 教师职称枚举转换
function changeTeacherPositionalEnum(positional) {
    if (positional == "PROFESSOR") {
        return "教授";
    } else if (positional == "ASSOCIATE_PROFESSOR") {
        return "副教授";
    } else if (positional == "LECTURER") {
        return "讲师"
    } else if (positional == "INSTRUCTOR") {
        return "辅导员";
    } else if (positional == "教授") {
        return "PROFESSOR";
    } else if (positional == "副教授") {
        return "ASSOCIATE_PROFESSOR";
    } else if (positional == "讲师") {
        return "LECTURER";
    } else if (positional == "辅导员") {
        return "INSTRUCTOR";
    }
}

// 跳转至详情
function toXqNormal(followId, type) {
    var followId = followId;
    var theType = type;
    sessionStorage.setItem("followId", JSON.stringify(followId));
    sessionStorage.setItem("type", JSON.stringify(theType));
    self.location.href = addUrl + "xq.html";
}

// 跳转至视频
function toXqVideo(followId, type) {
    var followId = followId;
    var theType = type;
    sessionStorage.setItem("followId", JSON.stringify(followId));
    sessionStorage.setItem("type", JSON.stringify(theType));
    window.open(addUrl + "video.html");
}